package com.linxiao.hrcrm.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.linxiao.hrcrm.domain.CrmWorksite;
import com.linxiao.hrcrm.service.ICrmCompanyService;
import com.linxiao.hrcrm.service.ICrmWorksiteService;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linxiao.hrcrm.mapper.CrmProjectMapper;
import com.linxiao.hrcrm.domain.CrmProject;
import com.linxiao.hrcrm.service.ICrmProjectService;

import javax.annotation.Resource;

/**
 * 项目信息Service业务层处理
 *
 * @author zhoudong
 * @date 2024-01-03
 */
@Service
public class CrmProjectServiceImpl implements ICrmProjectService {
    @Resource
    private CrmProjectMapper crmProjectMapper;
    @Resource
    private ICrmCompanyService crmCompanyService;
    @Resource
    private ICrmWorksiteService crmWorksiteService;

    /**
     * 查询项目信息
     *
     * @param id 项目信息主键
     * @return 项目信息
     */
    @Override
    public CrmProject selectCrmProjectById(Long id) {
        return crmProjectMapper.selectCrmProjectById(id);
    }

    /**
     * 查询项目信息列表
     *
     * @param crmProject 项目信息
     * @return 项目信息
     */
    @Override
    public List<CrmProject> selectCrmProjectList(CrmProject crmProject) {
        return crmProjectMapper.selectCrmProjectList(crmProject);
    }

    /**
     * 新增项目信息
     *
     * @param crmProject 项目信息
     * @return 结果
     */
    @Override
    public int insertCrmProject(CrmProject crmProject) {
        // 查询所属工地
        // checkWorksite(crmProject);
        checkParams(crmProject);

        crmProject.setCreateTime(DateUtils.getNowDate());
        return crmProjectMapper.insertCrmProject(crmProject);
    }

    /**
     * 参数验证
     * @param crmProject
     */
    private void checkParams(CrmProject crmProject) {
        if (crmProject.getStartTime() == null) {
            crmProject.setStartTime(DateUtils.getNowDate());
        }
        if (StrUtil.isBlank(crmProject.getStatus())) {
            crmProject.setStatus(CrmProject.STATUS_PROGRESS);
        }
        if (StrUtil.isBlank(crmProject.getSalaryUnit())) {
            crmProject.setSalaryUnit(CrmProject.SALARY_UNIT_HOUR);
        }
        if (CrmProject.SALARY_UNIT_HOUR.equals(crmProject.getSalaryUnit()) && StrUtil.isBlank(crmProject.getHourlyRateCalculation())) {
            crmProject.setHourlyRateCalculation(CrmProject.SALARY_CALCULATION_METHOD_ACCURATE);
        }

        if (CrmProject.SALARY_UNIT_DAY.equals(crmProject.getSalaryUnit()) || CrmProject.SALARY_UNIT_MOON.equals(crmProject.getSalaryUnit())) {
            if (!CrmProject.SALARY_CALCULATION_METHOD_ACCURATE.equals(crmProject.getHourlyRateCalculation())) {
                throw new UtilException("日薪和月薪只能选择精确计算");
            }
        }
    }

    /**
     * 验证工地
     * @param crmProject
     */
    private void checkWorksite(CrmProject crmProject) {
        if (crmProject.getWorksiteId() != null) {
            CrmWorksite crmWorksite = crmWorksiteService.selectCrmWorksiteById(crmProject.getWorksiteId());
            if (crmWorksite == null) {
                throw new UtilException("所属工地错误");
            }
            if (crmProject.getCompanyId() == null || crmProject.getCompanyId() == 0) {
                crmProject.setCompanyId(crmWorksite.getCompanyId());
            }
            crmProject.setAddress(crmWorksite.getProvince() + crmWorksite.getCity() + crmWorksite.getAddress());
        }
    }

    /**
     * 修改项目信息
     *
     * @param crmProject 项目信息
     * @return 结果
     */
    @Override
    public int updateCrmProject(CrmProject crmProject) {
        // checkWorksite(crmProject);
        crmProject.setUpdateTime(DateUtils.getNowDate());
        return crmProjectMapper.updateCrmProject(crmProject);
    }

    /**
     * 批量删除项目信息
     *
     * @param ids 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    public int deleteCrmProjectByIds(Long[] ids) {
        return crmProjectMapper.deleteCrmProjectByIds(ids);
    }

    /**
     * 删除项目信息信息
     *
     * @param id 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteCrmProjectById(Long id) {
        return crmProjectMapper.deleteCrmProjectById(id);
    }
}
